﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Charts;
using WpfChart.DataStruct;
using System.Collections.ObjectModel;

namespace WpfChart
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<StockData> stockObjColle = new ObservableCollection<StockData>();
        //ObservableCollection<Value> values = new ObservableCollection<Value>();

        public MainWindow()
        {
            InitializeComponent();
            //MyBindChart();
           
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //GetStockData();
            //CreateChart();
        }

        //public void GetStockData()
        //{
        //    List<StockData> lstData = new List<StockData>();
           
        //    StockData data1=new StockData(2454,2465,2470,2430,"2012-01-01");
        //    StockData data2 = new StockData(2465, 2462, 2465, 2435, "2012-02-01");
        //    StockData data3 = new StockData(2462, 2451, 2460, 2432, "2012-03-01");
        //    StockData data4 = new StockData(2451, 2461, 2472, 2432, "2012-04-01");
        //    StockData data5 = new StockData(2461, 2465, 2473, 2433, "2012-05-01");
        //    StockData data6 = new StockData(2465, 2463, 2474, 2434, "2012-06-01");
        //    StockData data7 = new StockData(2463, 2465, 2475, 2435, "2012-07-01");
        //    StockData data8 = new StockData(2455, 2467, 2476, 2436, "2012-08-01");
        //    StockData data9 = new StockData(2467, 2438, 2463, 2437, "2012-09-01");
        //    StockData data10 = new StockData(2438, 2454, 2456, 2438, "2012-10-01");
        //    StockData data11 = new StockData(2454, 2466, 2457, 2439, "2012-11-01");
        //    StockData data12 = new StockData(2466, 2445, 2447, 2423, "2012-12-01");
        //    StockData data13 = new StockData(2454, 2465, 2470, 2430, "2013-01-01");
        //    StockData data14 = new StockData(2465, 2462, 2465, 2435, "2013-02-01");
        //    StockData data15 = new StockData(2462, 2451, 2460, 2432, "2013-03-01");
        //    StockData data16 = new StockData(2451, 2461, 2472, 2432, "2013-04-01");
        //    StockData data17 = new StockData(2461, 2465, 2473, 2433, "2013-05-01");
        //    StockData data18 = new StockData(2465, 2463, 2474, 2434, "2013-06-01");
        //    StockData data19 = new StockData(2463, 2465, 2475, 2435, "2013-07-01");
        //    StockData data20 = new StockData(2455, 2467, 2476, 2436, "2013-08-01");
        //    StockData data21 = new StockData(2467, 2438, 2463, 2437, "2013-09-01");
        //    StockData data22 = new StockData(2438, 2454, 2456, 2438, "2013-10-01");
        //    StockData data23 = new StockData(2454, 2466, 2457, 2439, "2013-11-01");
        //    StockData data24 = new StockData(2466, 2445, 2447, 2423, "2013-12-01");
        //    StockData data25 = new StockData(2454, 2465, 2470, 2430, "2011-01-01");
        //    StockData data26 = new StockData(2465, 2462, 2465, 2435, "2011-02-01");
        //    StockData data27 = new StockData(2462, 2451, 2460, 2432, "2011-03-01");
        //    StockData data28 = new StockData(2451, 2461, 2472, 2432, "2011-04-01");
        //    StockData data29 = new StockData(2461, 2465, 2473, 2433, "2011-05-01");
        //    StockData data30 = new StockData(2465, 2463, 2474, 2434, "2011-06-01");
        //    StockData data31 = new StockData(2463, 2465, 2475, 2435, "2011-07-01");
        //    StockData data32 = new StockData(2455, 2467, 2476, 2436, "2011-08-01");
        //    StockData data33 = new StockData(2467, 2438, 2463, 2437, "2011-09-01");
        //    StockData data34 = new StockData(2438, 2454, 2456, 2438, "2011-10-01");
        //    StockData data35 = new StockData(2454, 2466, 2457, 2439, "2011-11-01");
        //    StockData data36 = new StockData(2466, 2445, 2447, 2423, "2011-12-01");

        //    lstData.AddRange(new StockData[] { data25,data26,data27,data28,data29,data30,
        //                                       data31,data32,data33,data34,data35,data36,
        //                                       data1, data2, data3, data4, data5, data6, data7, data8, data9, data10,
        //                                       data11, data12,data13,data14,data15,data16,data17,data18,data19,data20,
        //                                       data21,data22,data23,data24});
        //    foreach (StockData data in lstData)
        //        stockObjColle.Add(data);

        //    //return lstData;
        //}

        //public void CreateChart()
        //{
        //    //Chart chart = new Chart();
        //    //chart.Width = 700;
        //    //chart.Height = 300;
        //    //chart.Theme = "Theme2";
        //    //chart.IndicatorEnabled = true;
        //    //chart.ScrollingEnabled = false;
        //    //chart.ZoomingEnabled = true;
        //    //chart.ZoomOutText = "返回";
        //    //chart.ShowAllText = "退出";
        //    //chart.AnimatedUpdate = true;
           

        //    //Title title = new Title();
        //    //title.Text = "行情走势蜡烛图";
        //    //MyChart.Titles.Add(title);

        //    //x轴刻度
        //    //Axis axisX = new Axis();
        //    //axisX.IntervalType = IntervalTypes.Months;
        //    //axisX.ValueFormatString = "yyyy-MM-dd";
        //    //axisX.Interval = 6;
        //    //MyChart.AxesX.Add(axisX);

        //    ////y轴刻度
        //    //Axis axisY = new Axis();
        //    //axisY.AxisMaximum = 2490;
        //    //axisY.AxisMinimum = 2410;
        //    ////axisY.Prefix = "$";
        //    //MyChart.AxesY.Add(axisY);

        //    //添加蜡烛图形
        //    //DataSeries dataSeries = new DataSeries();
        //    //dataSeries.LegendText = "";
        //    //dataSeries.ToolTipText = "时间：#XValue\n开盘价: #Open\n收盘价: #Close\n最高价: #High\n最低价: #Low";
        //    //dataSeries.XValueType = ChartValueTypes.Date;
        //    //dataSeries.XValueFormatString = "yyyy-MM-dd";
        //    //dataSeries.RenderAs = RenderAs.CandleStick;
        //    //dataSeries.PriceDownColor = Brushes.Tomato;
        //    //dataSeries.PriceUpColor = Brushes.Green;
        //    //dataSeries.LineThickness = 1;
        //    //dataSeries.ShowInLegend = false;
        //    //dataSeries.MouseLeftButtonDown+=new MouseButtonEventHandler(dataSeries_MouseLeftButtonDown);
          
        //    //绘制蜡烛体
        //    //DataPoint dataPoint;
        //    //List<StockData> lstStock = stockObjColle.ToList();

        //    //for (int i = 0; i < lstStock.Count; i++)
        //    //{
        //    //    dataPoint = new DataPoint();
        //    //    dataPoint.XValue = lstStock[i].Time;
        //    //    dataPoint.MaxWidth = 5;
        //    //    double[] price = { lstStock[i].Open, lstStock[i].Close, lstStock[i].MaxPrcie, lstStock[i].MinPrice };
        //    //    dataPoint.YValues = price;
        //    //    dataSeries.DataPoints.Add(dataPoint);
        //    //}

        //    //dataSeries.DataSource = stockObjColle;
        //    //MyChart.Series[0].DataSource = stockObjColle;

        //    //绘制移动均线
        //    //DataSeries dataSeries2 = new DataSeries();
        //    //dataSeries2.XValueType = ChartValueTypes.Date;
        //    //dataSeries2.XValueFormatString = "yyyy-MM-dd";
        //    //dataSeries2.RenderAs = RenderAs.Line;
        //    //dataSeries2.MarkerType = Visifire.Commons.MarkerTypes.Circle;
        //    //dataSeries2.LightingEnabled = false; 
        //    //dataSeries2.LineThickness = 2;
        //    //dataSeries2.ShowInLegend = false;

        //    //for (int i = 0; i < lstStock.Count; i++)
        //    //{
        //    //    dataPoint = new DataPoint();
        //    //    dataPoint.XValue = lstStock[i].Time;
        //    //    dataPoint.YValue = lstStock[i].Open;
        //    //    dataSeries2.DataPoints.Add(dataPoint);
        //    //}

        //    //chart.Series.Add(dataSeries2);
        //    //LayoutRoot.Children.Add(chart);
        //}

        //public void MyBindChart()
        //{
        //    GetStockData();

        //    //x轴刻度
        //    Axis axisX = new Axis();
        //    axisX.IntervalType = IntervalTypes.Months;
        //    axisX.ValueFormatString = "yyyy-MM-dd";
        //    axisX.Interval = 6;
        //    MyChart.AxesX.Add(axisX);

        //    //y轴刻度
        //    Axis axisY = new Axis();
        //    axisY.AxisMaximum = 2490;
        //    axisY.AxisMinimum = 2410;
        //    //axisY.Prefix = "$";
        //    MyChart.AxesY.Add(axisY);
        //    MyChart.Series[0].DataSource = stockObjColle;

        //    //定时器
        //    //System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        //    //dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        //    //dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        //    //dispatcherTimer.Start();
        //}

        //private void dispatcherTimer_Tick(object sender, EventArgs e)
        //{
        //    DataSeries ds = MyChart.Series[0];
        //    MyChart.Dispatcher.BeginInvoke(new Action<DataSeries>(Update), new object[] { ds });
        //}

        ///// <summary>
        ///// 每个蜡烛的触发事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //void dataSeries_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    DataPoint dataPoint = sender as DataPoint;
        //    string strCategoryName = dataPoint.XValue.ToString();
        //    MessageBox.Show(strCategoryName + "\n");
        //    e.Handled = true;
        //}

        //private void btnUpdate_Click(object sender, RoutedEventArgs e)
        //{
        //    DataSeries ds = MyChart.Series[0];
        //    MyChart.Dispatcher.BeginInvoke(new Action<DataSeries>(Update), new object[] { ds });
        //}

        ///// <summary>
        ///// 动态更改数据
        ///// </summary>
        ///// <param name="ds"></param>
        //private void Update(DataSeries ds)
        //{
        //    if (ds != null)
        //    {
        //        Random rand = new Random();
        //        ObservableCollection<StockData> dataColle = ds.DataSource as ObservableCollection<StockData>;
        //        StockData data = dataColle[dataColle.Count - 1];
        //        data.Open = rand.Next(2410,2480);
        //        data.Close = rand.Next(2410, 2480);
        //        data.MinPrice = rand.Next(2410, 2460);
        //        data.MaxPrcie = rand.Next(2420, 2490);
        //        ds.DataPoints[dataColle.Count - 1].YValues = data.YScaleValues;
        //    }
        //}

   
    }
}
